# -*- coding:utf-8 -*-
from common.db import Record, session
from common.import_pyside import *
from common.data import DB
from utils.create_dir import create_case_dir
from utils.customORM import DoesNotExist
from utils.sendMsg import sendWinMsg


class PopupWindow(QDialog):
    def __init__(self):
        super(PopupWindow, self).__init__()
        self.setFixedSize(200, 100)
        self.setWindowTitle("任务名称")
        self.setWindowIcon(QIcon('./icon/system.ico'))
        h_layout = QVBoxLayout()
        label_title = QLabel("当前任务名称：")
        self.case_task_name_line_edit = QLineEdit()
        self.case_task_name_line_edit.setPlaceholderText("请输入任务名称！")
        self.case_task_name_line_edit.setFocus()
        self.case_task_name_line_edit.setMaxLength(100)
        push_button = QPushButton("开始录制")
        push_button.clicked.connect(self.do_push_button)
        h_layout.addWidget(label_title)
        h_layout.addWidget(self.case_task_name_line_edit)
        h_layout.addWidget(push_button)
        self.setLayout(h_layout)

    def do_push_button(self):
        DB.isRecord = True
        DB.case_task_name = self.case_task_name_line_edit.text()
        print(session.query(Record).filter_by(case_name=DB.case_task_name).all())

        # 查询到是否已有该任务名称
        if session.query(Record).filter_by(case_name=DB.case_task_name).all():
            sendWinMsg("错误", "该任务名已存在！！")
        else:
            # 创建case目录
            create_case_dir()
            instance = Record(case_name=DB.case_task_name)
            session.add(instance)
            session.commit()
            self.close()


if __name__ == '__main__':
    app = QApplication()
    win = PopupWindow()
    win.show()
    app.exec()
